package com.maxlen.camunda.service.impl;

import com.alibaba.fastjson.JSON;
import com.maxlen.camunda.constant.CamundaConstant;
import com.maxlen.camunda.entity.AttributeEntity;
import com.maxlen.camunda.entity.FormEntity;
import com.maxlen.camunda.entity.dto.SaveFormDTO;
import com.maxlen.camunda.mapper.AttributeMapper;
import com.maxlen.camunda.mapper.FormMapper;
import com.maxlen.camunda.service.FormService;
import com.tcloudit.common.core.web.domain.PageEntity;
import com.tcloudit.common.core.web.page.TableDataInfo;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class FormServiceImpl implements FormService {

    @Autowired
    private FormMapper formMapper;

    @Autowired
    private ProcessEngine processEngine;

    /**
     * 获取表单模型列表
     * @param page 分页搜索参数
     * @return 结果
     */
    @Override
    public TableDataInfo getList(PageEntity<String> page) {
        if(page.getPage() == null || page.getSize() == null){
            return null;
        }

        Integer count = formMapper.getCount(page);
        List<FormEntity> formEntityList = formMapper.getList(page);
        return new TableDataInfo(formEntityList, count);
    }

    /**
     * 获取状态为开启的表单模型列表
     * @param page 分页搜索参数
     * @return 结果
     */
    @Override
    public TableDataInfo getStartFormList(PageEntity<String> page) {
        if(page.getPage() == null || page.getSize() == null){
            return null;
        }

        Integer count = formMapper.getCountByStart(page);
        List<FormEntity> formEntityList = formMapper.getListByStart(page);
        return new TableDataInfo(formEntityList, count);
    }

    /**
     * 获取表单模型详情
     * @param id id
     * @return 结果
     */
    @Override
    public FormEntity info(String id) {
        return formMapper.info(id);
    }

    /**
     * 新增表单模型
     * @param formEntity 实体类
     * @return 结果
     */
    @Override
    @Transactional
    public int add(FormEntity formEntity) {
        String formId = formEntity.getFormId();
        Deployment deploy = processEngine
                .getRepositoryService()
                .createDeployment()
                .addString(formId + "." + CamundaConstant.FORM,
                        CamundaConstant.DEFAULT_FORM.replace(CamundaConstant.REPLACE, formId))
                .deploy();

        formEntity.setDeploymentId(deploy.getId());

        return formMapper.add(formEntity);
    }

    /**
     * 修改表单模型
     * @param formEntity 实体类
     * @return 结果
     */
    @Override
    public int edit(FormEntity formEntity) {
        return formMapper.edit(formEntity);
    }

    /**
     * 删除表单模型
     * @param ids 部署id集合
     * @return 结果
     */
    @Override
    @Transactional
    public int remove(String[] ids) {
        int remove = formMapper.remove(ids);
        for (String id : ids) {
            processEngine.getRepositoryService().deleteDeployment(id, true);
        }
        return remove;
    }

    /**
     * 根据表单标识获取记录数
     * @param formEntity 表单标识和ID
     * @return 结果
     */
    @Override
    public int getCountByFormId(FormEntity formEntity) {
        return formMapper.getCountByFormId(formEntity);
    }

    /**
     * 根据部署ID获取表单数据
     * @param deploymentId 部署ID
     * @return 结果
     */
    @Override
    public String getFormByte(String deploymentId) {
        List<Resource> deploymentResources = processEngine
                .getRepositoryService()
                .getDeploymentResources(deploymentId);
        for (Resource deploymentResource : deploymentResources) {
            return new String(deploymentResource.getBytes());
        }
        return null;
    }

    /**
     * 保存表单信息
     * @param saveFormDTO 表单json和表单id
     * @return 结果
     */
    @Override
    public int saveForm(SaveFormDTO saveFormDTO) {
        FormEntity formEntity = formMapper.info(saveFormDTO.getId());

        String formId = formEntity.getFormId();
        Deployment deploy = processEngine
                .getRepositoryService()
                .createDeployment()
                .addString(formId + '.' + CamundaConstant.FORM, JSON.parse(saveFormDTO.getForm()).toString())
                .deploy();

        processEngine.getRepositoryService().deleteDeployment(formEntity.getDeploymentId(), true);

        formEntity.setDeploymentId(deploy.getId());
        return formMapper.edit(formEntity);
    }
}
